package com.ssm.controller;

import com.ssm.pojo.Customer;
import com.ssm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/list")
    public String list(){
        return "customer/customer";
    }

    @RequestMapping("/listdata")
    @ResponseBody
    public Map<String,Object> listData(Integer page, Integer limit,String customerNO){
        List<Customer> customers = customerService.seleteCustomersPage(customerNO,page,limit);
        Integer count = customerService.selectSize(customerNO);

        // 返回表格数据
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count",count);
        resultMap.put("data",customers);
        return resultMap;
    }

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String  to_add(){
        return "customer/customer_new";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(Customer customer){
        customerService.insert(customer);
        return "customer/customer";
    }

    @RequestMapping(value = "update",method = RequestMethod.GET)
    public String to_update(Model model,String customerNO){
        Customer customer = customerService.selectCustomerByNO(customerNO);
        model.addAttribute("customer",customer);
        return "customer/customer_update";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(Customer customer){
        customerService.update(customer);
        return "customer/customer";
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(String customerNO){
        customerService.deleteByCustomerNO(customerNO);

        HashMap<String,String> maps = new HashMap<String, String>();
        maps.put("status","true");
        maps.put("msg","删除成功");
        return maps;
    }
}
